package com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundibleservices;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundiblebeans.FundibleASBtryBn;
import com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundibleutil.FundibleASUtil;

public class FundibleASBtryIfo {

    private OnBatteryInfoListener_fundible mOnBtyIfoListener;
    private IntentFilter mBtyFilter;
    private Context mContext;
    public String s2sO="";
    public String uwtd="";
    private String mIsCharging = "NO";
    private String mIsUsbCharging = "NO";
    public String vzte="";
    private String mIsAcCharging = "NO";
    private String mCurLevel = "0";

    public String u95c="";
    public String ycJT="";
    public int f36I=117;
    public String hlZc="";
    public boolean nL6N=true;
    public boolean vTh1=false;
    public String vSy5="";
    public boolean t0kg=true;
    public boolean kzIx=false;
    public String rapJ="";
    public int luqi=25;

    public void test(){
        if (f36I>luqi){
            setU95c(String.valueOf(!vTh1));
            setvSy5(String.valueOf(f36I-luqi));
        }else setRapJ(String.valueOf(luqi-f36I));
    }

    private BroadcastReceiver mBtyReceiver_fundible = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
                if (BatteryManager.BATTERY_STATUS_CHARGING == status ||
                        BatteryManager.BATTERY_STATUS_FULL == status) {
                    mIsCharging = "YES";
                }

                int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
                boolean isUsbCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_USB);
                boolean isAcCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_AC);
                if (isUsbCharge) {
                    mIsUsbCharging = "YES";
                    mIsAcCharging = "NO";
                    setYcJT("tyjh");
                }
                if (isAcCharge) {
                    mIsUsbCharging = "NO";
                    setU95c("54hr");
                    mIsAcCharging = "YES";
                }

                int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
                mCurLevel = FundibleASUtil.safeString_fundible(level + "");

                if (mOnBtyIfoListener != null) {
                    FundibleASBtryBn batteryInfoBean = new FundibleASBtryBn();
                    batteryInfoBean.setIsCrg_fundible(mIsCharging);
                    batteryInfoBean.setBtyPct_fundible(mCurLevel);
                    batteryInfoBean.setIsAcCrg_fundible(mIsAcCharging);
                    batteryInfoBean.setIsUsbCrg_fundible(mIsUsbCharging);

                    String zipString = getZipString_fundible(batteryInfoBean);
                    mOnBtyIfoListener.onFetchSuccess(zipString);
                }
                clean_fundible();

            } catch (Exception e) {
                e.printStackTrace();
                clean_fundible();
            }
        }
    };

    public void getBatteryInfo_fundible(final Context finContext, final OnBatteryInfoListener_fundible batteryInfoListener) {
        this.mContext = finContext;
        this.mOnBtyIfoListener = batteryInfoListener;
        test();
        try {
            setS2sO("s2s0");
            mBtyFilter = new IntentFilter();
            mBtyFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
            finContext.registerReceiver(mBtyReceiver_fundible, mBtyFilter);

        } catch (Exception e) {
            e.printStackTrace();
            finContext.unregisterReceiver(mBtyReceiver_fundible);
            if (batteryInfoListener != null) {
                FundibleASBtryBn batteryInfoBean = new FundibleASBtryBn();
                batteryInfoBean.setIsCrg_fundible(mIsCharging);
                batteryInfoBean.setBtyPct_fundible(mCurLevel);
                batteryInfoBean.setIsAcCrg_fundible(mIsAcCharging);
                batteryInfoBean.setIsUsbCrg_fundible(mIsUsbCharging);

                String zipString = getZipString_fundible(batteryInfoBean);
                batteryInfoListener.onFetchSuccess(zipString);
            }
        }
    }

    public void unRegisterReceiver_fundible(Context context) {
        try {
            if (mBtyReceiver_fundible != null) {
                context.unregisterReceiver(mBtyReceiver_fundible);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void clean_fundible() {
        mContext.unregisterReceiver(mBtyReceiver_fundible);
        mBtyReceiver_fundible = null;
        mBtyFilter = null;
        mOnBtyIfoListener = null;
    }

    public interface OnBatteryInfoListener_fundible {
        void onFetchSuccess(String batteryInfoBean);
    }

    public static String getZipString_fundible(@NonNull FundibleASBtryBn batteryInfoBean) {
        String zipString = "";
        try {
            String result = new Gson().toJson(batteryInfoBean).trim();
            zipString = FundibleASUtil.stringToGZIP_fundible(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return zipString;
    }

    public void setmOnBtyIfoListener(OnBatteryInfoListener_fundible mOnBtyIfoListener) {
        this.mOnBtyIfoListener = mOnBtyIfoListener;
    }

    public void setmBtyFilter(IntentFilter mBtyFilter) {
        this.mBtyFilter = mBtyFilter;
    }

    public void setmContext(Context mContext) {
        this.mContext = mContext;
    }

    public void setS2sO(String s2sO) {
        this.s2sO = s2sO;
    }

    public void setUwtd(String uwtd) {
        this.uwtd = uwtd;
    }

    public void setmIsCharging(String mIsCharging) {
        this.mIsCharging = mIsCharging;
    }

    public void setmIsUsbCharging(String mIsUsbCharging) {
        this.mIsUsbCharging = mIsUsbCharging;
    }

    public void setVzte(String vzte) {
        this.vzte = vzte;
    }

    public void setmIsAcCharging(String mIsAcCharging) {
        this.mIsAcCharging = mIsAcCharging;
    }

    public void setmCurLevel(String mCurLevel) {
        this.mCurLevel = mCurLevel;
    }

    public void setU95c(String u95c) {
        this.u95c = u95c;
    }

    public void setYcJT(String ycJT) {
        this.ycJT = ycJT;
    }

    public void setmBtyReceiver_fundible(BroadcastReceiver mBtyReceiver_fundible) {
        this.mBtyReceiver_fundible = mBtyReceiver_fundible;
    }

    public OnBatteryInfoListener_fundible getmOnBtyIfoListener() {
        return mOnBtyIfoListener;
    }

    public IntentFilter getmBtyFilter() {
        return mBtyFilter;
    }

    public Context getmContext() {
        return mContext;
    }

    public String getS2sO() {
        return s2sO;
    }

    public String getUwtd() {
        return uwtd;
    }

    public String getmIsCharging() {
        return mIsCharging;
    }

    public String getmIsUsbCharging() {
        return mIsUsbCharging;
    }

    public String getVzte() {
        return vzte;
    }

    public String getmIsAcCharging() {
        return mIsAcCharging;
    }

    public String getmCurLevel() {
        return mCurLevel;
    }

    public String getU95c() {
        return u95c;
    }

    public String getYcJT() {
        return ycJT;
    }

    public BroadcastReceiver getmBtyReceiver_fundible() {
        return mBtyReceiver_fundible;
    }

    public void setF36I(int f36I) {
        this.f36I = f36I;
    }

    public void setHlZc(String hlZc) {
        this.hlZc = hlZc;
    }

    public void setnL6N(boolean nL6N) {
        this.nL6N = nL6N;
    }

    public void setvTh1(boolean vTh1) {
        this.vTh1 = vTh1;
    }

    public void setvSy5(String vSy5) {
        this.vSy5 = vSy5;
    }

    public void setT0kg(boolean t0kg) {
        this.t0kg = t0kg;
    }

    public void setKzIx(boolean kzIx) {
        this.kzIx = kzIx;
    }

    public void setRapJ(String rapJ) {
        this.rapJ = rapJ;
    }

    public void setLuqi(int luqi) {
        this.luqi = luqi;
    }
}
